﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CooStationsToolsNew.Model;
using System.Data;
using System.Xml;

namespace CooStationsToolsNew
{
    class EventTools
    {
        public EventTools()
        {
        }

        double LastTime = 0;
        double LastDelay = 0;
        string LastId = "";
        /// <summary>
        /// 删除一个ACTION
        /// </summary>
        /// <param name="itemid"></param>
        /// <returns></returns>
        public void DeleteActionToData(string itemid)
        {
            for (int i = 0; i < App.eventdt.Rows.Count; i++)
            {
                string id = App.eventdt.Rows[i][0].ToString();
                if (id == itemid)
                {
                    App.eventdt.Rows.RemoveAt(i);
                    break;
                }
            }
            SaveEventToXml(App.appPath + "//temp//" + App.pageName + "_event.xml");
            SaveActionListToXml(App.appPath + "//temp//" + App.pageName + "_event.xml");
        }

        /// <summary>
        /// 保存action到DT
        /// </summary>
        /// <param name="itemid"></param>
        /// <param name="actionlist"></param>
        /// <param name="TargetId"></param>
        public void SaveActionToData(string itemid, List<ClipAction> actionlist)
        {
            bool isHasItem = false;
            DataRow Mydr = null;
            for (int i = 0; i < App.eventdt.Rows.Count; i++)
            {
                string id = App.eventdt.Rows[i][0].ToString();
                if (id == itemid)
                {
                    isHasItem = true;
                    //List<ClipAction> mylist = (List<ClipAction>)App.eventdt.Rows[i][1];
                    Mydr = App.eventdt.Rows[i];
                    Mydr[1] = actionlist;
                    break ;
                }
            }
            if (isHasItem == false)
            {
                DataRow dr = App.eventdt.NewRow();
                dr[0] = itemid;
                dr[1] = actionlist;
                App.eventdt.Rows.Add(dr);
            }
            SaveEventToXml(App.appPath + "//temp//"+App.pageName+"_event.xml");
            SaveActionListToXml(App.appPath + "//temp//" + App.pageName + "_event.xml");
        }
        /// <summary>
        /// 得到一个ItemList
        /// </summary>
        /// <param name="ItemID"></param>
        /// <returns></returns>
        public DataTable GetItemList(string ItemID)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("TargetID");
            dt.Columns.Add(new DataColumn("objectList", typeof(List<Model.ClipAction>)));
            List<ClipAction> calist = new List<ClipAction>();
            for (int i = 0; i < App.eventdt.Rows.Count; i++)
            {
                if (App.eventdt.Rows[i][0].ToString() == ItemID)
                {
                    List<ClipAction> im = (List<ClipAction>)App.eventdt.Rows[i][1];
                    for (int j = 0; j < im.Count; j++)
                    {
                        im[j].Mark = j.ToString();
                        ClipAction b = (ClipAction)im[j];
                        if (calist.Count == 0)
                        {
                            calist.Add(b);
                        }
                        else if (calist.Count > 0)
                        {
                            bool bott = false;
                            for (int h = 0; h < calist.Count; h++)
                            {
                                if (calist[h].TargetId == b.TargetId)
                                {
                                    bott = true;
                                    break;
                                }

                            }
                            if (!bott)
                            {
                                calist.Add(b);
                            }
                            else
                            {
                                bott = false;
                            }
                        }
                    }
                }
            }
            for (int i = 0; i < calist.Count; i++)
            {
                List<Model.ClipAction> ca = new List<ClipAction>();
                ca = GetClipActionList(ItemID,calist[i].TargetId);
                DataRow dr =dt.NewRow();
                dr[0] = calist[i].TargetId;
                dr[1] = ca;
                dt.Rows.Add(dr);
            }
            return dt;
        }
        /// <summary>
        /// 将DT数据存入XML
        /// </summary>
        /// <param name="xmlpath"></param>
        /// <returns></returns>
        public bool SaveEventToXml(string xmlpath)
        {
            try
            {
                XmlDocument xdoc = new XmlDocument();
                xdoc.Load(xmlpath);
                XmlNode xe = xdoc.ChildNodes[1].ChildNodes[0];
                xe.RemoveAll();

                for (int i = 0; i < App.eventdt.Rows.Count; i++)
                {
                    string id = App.eventdt.Rows[i][0].ToString();
                    XmlElement xeid = xdoc.CreateElement("item");
                    xeid.SetAttribute("id", id);
                    xe.AppendChild(xeid);
                    List<ClipAction> actionlist = (List<ClipAction>)App.eventdt.Rows[i][1];
                    for (int j = 0; j < actionlist.Count; j++)
                    {
                        ClipAction ca = (ClipAction)actionlist[j];
                        XmlElement xeAction = xdoc.CreateElement("clipAction");
                        if (ca.TargetId != LastId)
                        {
                            LastDelay = 0;
                            LastTime = 0;
                        }

                        if (ca.TargetId != null && ca.TargetId != "")
                        {
                            xeAction.SetAttribute("targetId", ca.TargetId);

                            xeAction.SetAttribute("tweenTarget", GetActionString(ca, j));
                            xeid.AppendChild(xeAction);
                        }
                    }
                    List<string> scriptlist = (List<string>)App.scriptdt.Rows[i][1];
                    for (int j = 0; j < scriptlist.Count; j++)
                    {
                        string name = scriptlist[j].ToString();
                        XmlElement xeAction = xdoc.CreateElement("scriptFunction");
                        xeAction.SetAttribute("functionName", name);
                        xeid.AppendChild(xeAction);
                    }

                }
                xdoc.Save(xmlpath);
                return true;
            }
            catch (Exception)
            {
                return false;
            }

        }

        /// <summary>
        /// 保存scriptlist到DT
        /// </summary>
        /// <param name="itemid"></param>
        /// <param name="scriptlist"></param>
        public void SaveScriptToDt(string itemid, List<string> scriptlist)
        {
            bool isHasItem = false;
            DataRow Mydr = null;
            try
            {


               
                    for (int i = 0; i < App.scriptdt.Rows.Count; i++)
                    {
                        string id = App.scriptdt.Rows[i][0].ToString();
                        if (id == itemid)
                        {
                            isHasItem = true;
                            Mydr = App.scriptdt.Rows[i];
                            Mydr[1] = scriptlist;
                            break;
                        }
                    }


                    if (isHasItem == false)
                    {
                        DataRow dr = App.scriptdt.NewRow();
                        dr[0] = itemid;
                        dr[1] = scriptlist;
                        App.scriptdt.Rows.Add(dr);
                    }
                    SaveEventToXml(App.appPath + "//temp//" + App.pageName + "_event.xml");
               
            }
            catch { }
        }


        /// <summary>
        /// 将A标签的列表存入XML中
        /// </summary>
        /// <param name="xmlpath"></param>
        /// <returns></returns>
        public bool SaveActionListToXml(string xmlpath)
        {
            try
            {
                if (App.ActionList.Count > 0)
                {
                    XmlDocument xdoc = new XmlDocument();
                    xdoc.Load(xmlpath);
                    XmlNodeList list = xdoc.ChildNodes[1].ChildNodes[0].ChildNodes;

                    foreach (XmlNode xn in list)
                    {
                        XmlElement xe = (XmlElement)xn;
                        if (xe.HasChildNodes)
                        {
                            foreach (XmlNode xn1 in xe.ChildNodes)
                            {
                                XmlElement xe1 = (XmlElement)xn1;
                                if (xe1.Name == "a")
                                {
                                    xe.RemoveChild(xe1);
                                }
                            }
                        }
                    }

                    foreach (XmlNode xn in list)
                    {
                        XmlElement xe = (XmlElement)xn;
                        for (int i = 0; i < App.ActionList.Count; i++)
                        {
                            CooStationsToolsNew.Model.Action action = (CooStationsToolsNew.Model.Action)App.ActionList[i];
                            if (xe.GetAttribute("id") == action.Itemid)
                            {
                                XmlElement xe2 = xdoc.CreateElement("a");
                                xe2.SetAttribute("url", action.Url);
                                xe2.SetAttribute("loadType", action.LoadType);
                                xe2.SetAttribute("tweenSelfPage", SettweenSelfPage(action));
                                xe2.SetAttribute("tweenTargetPage", SettweenTargetPage(action));
                                xe.PrependChild(xe2);
                            }
                        }
                    }
                    xdoc.Save(xmlpath);
                }

                return true;
            }
            catch
            {
                return false;
            }
        }

        private void SetAction(XmlElement xmnl, CooStationsToolsNew.Model.Action action, XmlDocument xdoc)
        {
               XmlElement xe2  =  xdoc.CreateElement("a");
               xe2.SetAttribute("url", action.Url);
               xe2.SetAttribute("loadType", action.LoadType);
               xe2.SetAttribute("tweenSelfPage", SettweenSelfPage(action));
               xe2.SetAttribute("tweenTargetPage", SettweenTargetPage(action));
               xmnl.PrependChild(xe2);
            
        }

        /// <summary>
        /// 将SelfPage属性拼成字符串
        /// </summary>
        /// <param name="ca"></param>
        /// <returns></returns>
        private string SettweenSelfPage(CooStationsToolsNew.Model.Action ca)
        {
            string tweenTarget = "";
            if (ca.XSelf != null && ca.IsXSelf == true)
            {
                tweenTarget += "x:" + ca.XSelf + "|";
            }
            if (ca.YSelf != null && ca.IsYSelf == true)
            {
                tweenTarget += "y:" + ca.YSelf + "|";
            }
            if (ca.TimeSelf != null && ca.IsTimerSelf == true)
            {
                tweenTarget += "time:" + ca.TimeSelf + "|";
            }
            if (ca.AlphaSelf != null && ca.IsAlphaSelf == true)
            {
                tweenTarget += "alpha:" + ca.AlphaSelf + "|";
            }
            if (ca.DelaySelf != null && ca.IsDelaySelf == true)
            {
                tweenTarget += "delay:" + ca.DelaySelf + "|";
            }
            if (ca.OverwriteSelf != null)
            {
                tweenTarget += "overwrite:" + ca.OverwriteSelf + "|";
            }
            if (ca.WidthSelf != null && ca.IsWidthSelf == true)
            {
                tweenTarget += "width:" + ca.WidthSelf + "|";
            }
            if (ca.HeightSelf != null && ca.IsHeightSelf == true)
            {
                tweenTarget += "height:" + ca.HeightSelf + "|";
            }
            if (ca.XscaleSelf != null)
            {
                tweenTarget += "xscale:" + ca.XscaleSelf + "|";
            }
            if (ca.YscaleSelf != null)
            {
                tweenTarget += "yscale:" + ca.YscaleSelf + "|";
            }
            if (ca.RotationSelf != null)
            {
                tweenTarget += "rotation:" + ca.RotationSelf + "|";
            }
            if (ca.EaseSelf != null)
            {
                tweenTarget += "ease:" + ca.EaseSelf + "|";
            }
            if (tweenTarget.Length > 0)
            {
                tweenTarget = tweenTarget.Substring(0, tweenTarget.Length - 1);
            }
            else {
                tweenTarget = "";
            }
            return tweenTarget;
        }

        /// <summary>
        /// 将TargetPage属性拼成字符串
        /// </summary>
        /// <param name="ca"></param>
        /// <returns></returns>
        private string SettweenTargetPage(CooStationsToolsNew.Model.Action ca)
        {
            string tweenTarget = "";
            if (ca.X != null && ca.IsX == true)
            {
                tweenTarget += "x:" + ca.X + "|";
            }
            if (ca.Y != null && ca.IsY == true)
            {
                tweenTarget += "y:" + ca.Y + "|";
            }
            if (ca.Time != null && ca.IsTimer == true)
            {
                tweenTarget += "time:" + ca.Time + "|";
            }
            if (ca.Alpha != null && ca.IsAlpha == true)
            {
                tweenTarget += "alpha:" + ca.Alpha + "|";
            }
            if (ca.Delay != null && ca.IsDelay == true)
            {
                tweenTarget += "delay:" + ca.Delay + "|";
            }

            if (ca.Overwrite != null)
            {
                tweenTarget += "overwrite:" + ca.Overwrite + "|";
            }
            if (ca.Width != null && ca.IsWidth == true)
            {
                tweenTarget += "width:" + ca.Width + "|";
            }
            if (ca.Height != null && ca.IsHeight == true)
            {
                tweenTarget += "height:" + ca.Height + "|";
            }
            if (ca.Xscale != null)
            {
                tweenTarget += "xscale:" + ca.Xscale + "|";
            }
            if (ca.Yscale != null)
            {
                tweenTarget += "yscale:" + ca.Yscale + "|";
            }
            if (ca.Rotation != null)
            {
                tweenTarget += "rotation:" + ca.Rotation + "|";
            }
            if (ca.Ease != null)
            {
                tweenTarget += "ease:" + ca.Ease + "|";
            }
            if (tweenTarget.Length > 0)
            {
                tweenTarget = tweenTarget.Substring(0, tweenTarget.Length - 1);
            }
            else {
                tweenTarget = "";
            }
            return tweenTarget;
        }

        /// <summary>
        /// 根据对象的属性获得存入XML的字符串
        /// </summary>
        /// <param name="ca"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public string GetActionString(ClipAction ca, int count)
        {
            double thisDelay = 0;
            string tweenTarget = "";
            if (ca.X != null && ca.IsX == true)
            {
                tweenTarget += "x:" + ca.X + "|";
            }
            if (ca.Y != null && ca.IsY == true)
            {
                tweenTarget += "y:" + ca.Y + "|";
            }
            if (ca.Time != null && ca.IsTimer == true)
            {

               tweenTarget += "time:" + ca.Time + "|";

            }
            if (ca.Alpha != null && ca.IsAlpha == true)
            {
                tweenTarget += "alpha:" + ca.Alpha + "|";
            }
            if (ca.Delay != null && ca.IsDelay == true)
            {
                if (ca.TargetId != LastId)
                {
                    tweenTarget += "delay:" + ca.Delay + "|";
                    thisDelay = double.Parse(ca.Delay);

                }
                else
                {
                     thisDelay = LastTime + double.Parse(ca.Delay) + LastDelay;
                    tweenTarget += "delay:" + thisDelay + "|";

                }

            }

            if (ca.Overwrite != null)
            {
                tweenTarget += "overwrite:" + ca.Overwrite + "|";
            }
            if (ca.Width != null && ca.IsWidth == true)
            {
                tweenTarget += "width:" + ca.Width + "|";
            }
            if (ca.Height != null && ca.IsHeight == true)
            {
                tweenTarget += "height:" + ca.Height + "|";
            }
            if (ca.Xscale != null)
            {
                tweenTarget += "xscale:" + ca.Xscale + "|";
            }
            if (ca.Yscale != null)
            {
                tweenTarget += "yscale:" + ca.Yscale + "|";
            }
            if (ca.Rotation != null)
            {
                tweenTarget += "rotation:" + ca.Rotation + "|";
            }
            if (ca.Ease != null)
            {
                tweenTarget += "ease:" + ca.Ease + "|";
            }
            tweenTarget = tweenTarget.Substring(0, tweenTarget.Length - 1);
            LastId = ca.TargetId;
            LastTime = double.Parse(ca.Time);
            LastDelay = thisDelay;
            return tweenTarget;
        }
        /// <summary>
        /// 根据ItemID和targetId查找一个list
        /// </summary>
        /// <param name="ItemID"></param>
        /// <param name="targetId"></param>
        /// <returns></returns>
        public List<ClipAction> GetClipActionList(string ItemID, string targetId)
        {
            List<ClipAction> calist = new List<ClipAction>();
            for (int i = 0; i < App.eventdt.Rows.Count; i++)
            {
                if (App.eventdt.Rows[i][0].ToString() == ItemID)
                {
                    List<ClipAction> im = (List<ClipAction>)App.eventdt.Rows[i][1];
                    for (int h = 0; h < im.Count; h++)
                    {
                        ClipAction ca = im[h];
                        if (ca.TargetId == targetId)
                        {
                            calist.Add(ca);
                        }
                    }
                }
            }
            return calist;
        }
    }
}
